import java.util.*;
public class Byte {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while(T-->0){
            boolean ans = true;
            int N = scanner.nextInt();  //网球桶数
            ArrayList<Deque<Integer>> balls = new ArrayList<>();
            int all_K = 0;
            for(int i=0;i<N;i++){
                int k_i = scanner.nextInt(); //表示第i个桶有几个网球
                all_K += k_i;
                Deque<Integer> queue = new LinkedList<>();
                balls.add(queue); //添加
                for(int j =0;j<k_i;j++){
                    balls.get(i).addLast(scanner.nextInt()); //末尾插入
                }
            }
            for(int i=all_K;i>=1;i--){
                boolean flag = false;  //检测序号i是否存在
                //队列不空
                for(int j=0;j<N;j++){
                    Deque<Integer> q = balls.get(j);
                    if(!q.isEmpty()){
                        if(q.peekFirst()==i){  //判断头部情况
                            q.pollFirst();
                            flag = true;
                        }else if(q.peekLast()==i){  //判断尾部情况
                            q.pollLast();
                            flag = true;
                        }
                    }
                }
                if(!flag){
                    ans = false;
                    break;
                }

            }

            if(ans){
                System.out.println("1");
            }else{
                System.out.println("0");
            }

        }

    }
}
